package com.david.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.david.dao.LogMapper;
import com.david.dao.UserInfoMapper;
import com.david.entity.LogDetail;
import com.david.entity.LogMessage;
import com.david.entity.OperateLog;
import com.david.service.LogInfoInterface;
import com.david.vo.CardBean;
import com.david.vo.ConsoleBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LogInfoInterfaceImpl implements LogInfoInterface {

    @Resource
    LogMapper logMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public CardBean getLog(Integer page, Integer limit) {
        CardBean cardBean = new CardBean();
        cardBean.setCode(0);
        cardBean.setMsg("获取成功！");

        PageHelper.startPage(page, limit);
        List<LogMessage> logMessageList = logMapper.getLog();
        cardBean.setData(logMessageList);

        PageInfo<LogMessage> temp = new PageInfo<>(logMessageList);
        cardBean.setCount((int)temp.getTotal());

        return cardBean;
    }

    @Override
    public CardBean getWeb() throws NullPointerException{
        ConsoleBean consoleBean = new ConsoleBean();
        consoleBean.setMessage("获取成功！");
        consoleBean.setStatus(1);

        CardBean cardBean = new CardBean();
        Map<String, String> temp = new HashMap<>();
        // 获取当前在线人数，这儿获取redis中的session数量来写
        long onlineUser = stringRedisTemplate.opsForSet().size("onlineSet") - 1;
        temp.put("value1", String.valueOf(onlineUser));
        // 获取今日访问
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sdf.format(date).substring(0, 11) + "00:00:00";
        temp.put("value2", String.valueOf(logMapper.getCountCurrentTime(time)));
        temp.put("value3", String.valueOf(logMapper.getLog().size()));
        temp.put("value4", String.valueOf(userInfoMapper.getAllUser().size()));

        cardBean.success(temp);

        return cardBean;
    }

    @Override
    public CardBean getLogDetail(Integer page, Integer limit){
        CardBean cardBean = new CardBean();
        cardBean.setMsg("获取成功！");
        cardBean.setCode(0);

        PageHelper.startPage(page, limit);
        List<LogDetail> temp = logMapper.getLogDetail();
        cardBean.setData(temp);
        PageInfo<LogDetail> logDetailPageInfo = new PageInfo<>(temp);

        // 如果是超级管理员，就给看日志
        if("超级管理员".equals(userInfoMapper.findByUsername(StpUtil.getLoginId().toString()).getUserface())){
            cardBean.setCount((int)(logDetailPageInfo.getTotal()));
        }else{
            cardBean.setCount(0);
        }

        return cardBean;
    }

    @Override
    public CardBean getOperateLog(Integer page, Integer limit){
        CardBean cardBean = new CardBean();
        cardBean.setMsg("获取成功！");
        cardBean.setCode(0);

        log.info("调用成功！！！！！！！");
        PageHelper.startPage(page, limit);
        List<OperateLog> temp = logMapper.getOperateLog();
        cardBean.setData(temp);
        PageInfo<OperateLog> logDetailPageInfo = new PageInfo<>(temp);
        cardBean.setCount((int)(logDetailPageInfo.getTotal()));

        return cardBean;
    }
}
